"""author: wangxin
   data: 2021/12/13
   project: wangxin
"""
# 回合制对战游戏
# 创建角色：名字、性别、血量、装备
# 创建房间：角色可加入房间，房间内的人可互打
# 房间上限2人 战斗 装备-防弹衣
# 血量默认100
# 正常被攻击-10滴血
# 有防弹衣-5滴血
from time import sleep

class Character:
    def __init__(self, name, sex, blood=100):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.room = None
        self.equip = None
    def attack(self,other):
        if other.equip == None:
            self.damage = 10
        else:
            self.damage = 5
        print(f"{self.name}攻击了{other.name}{self.damage}点血量")
        other.blood -= self.damage

class Room:
    def __init__(self, name):
        self.name = name
        self.member = []
    def JoinRoom(self, character):
        if character.room == None:
            if len(self.member) < 2:
                self.member.append(character)
                character.room = self.name
                print(f"{character.name}已成功加入{self.name}房间")
            else:
                print(f"{self.name}房间人数已达上限")
        else:
            print(f"{character.name}已在{self.name}房间,不能重复加入")

characters = {}
rooms = {}
print("#"*20)
print("1、创建角色")
print("2、创建房间")
print("3、加入房间")
print("4、开始战斗")
print("5、退出游戏")
print("#"*20)
while 1:
    n = input("请输入你的选择：")
    if n.isdigit():
        option = int(n)
        if option == 1:
            while 1:
                name = input("请输入角色名字：")
                if name in characters:
                    print(f"{name}已存在，请重新输入")
                else:
                    sex = input("请输入角色性别：")
                    characters[name] = Character(name,sex)
                    print(f"角色{name}创建成功")
                    break
        elif option == 2:
            while 1:
                room_name = input("请输入房间名：")
                if room_name in rooms:
                    print(f"{room_name}已存在，请重新输入")
                else:
                    rooms[room_name] = Room(room_name)
                    print(f"房间{room_name}创建成功")
                    break
        elif option == 3:
            if rooms == {}:
                print("目前没有房间，快去创建一个房间吧！")
            else:
                print(f"已有角色：{characters.keys()}")
                print(f"已有房间：{rooms.keys()}")
                while 1:
                    character = input("请输入你的角色：")
                    room = input("请输入你要加入的房间：")
                    if character in characters and room in rooms:
                        rooms[room].JoinRoom(characters[character])
                        break
                    else:
                        print("输入有误，请重新输入")
        elif option == 4:
            while 1:
                print(f"已有房间：{rooms.keys()}")
                attckroom = input("请输入房间号(按q退出):")
                if attckroom != 'q':
                    if attckroom in rooms:
                        if len(rooms[attckroom].member) == 2:
                            while 1:
                                if rooms[attckroom].member[0].blood <= 0:
                                    print(f"{rooms[attckroom].member[0].name}被击败，游戏结束")
                                    break
                                elif rooms[attckroom].member[1].blood <= 0:
                                    print(f"{rooms[attckroom].member[1].name}被击败，游戏结束")
                                    break
                                else:
                                    rooms[attckroom].member[0].attack(rooms[attckroom].member[1])
                                    sleep(1)
                                    rooms[attckroom].member[1].attack(rooms[attckroom].member[0])
                        else:
                            print(f"{attckroom}房间人数不足，无法开始战斗，请重新选择")
                    else:
                        print(f"{attckroom}房间不存在，请重新选择：")
                else:
                    break
        elif option == 5:
            print("已退出")
            break
        else:
            print("输入有误，请重新输入")
    else:
        print("输入有误，请重新输入")